package org.liquidByte.trafficMustang.androidApp.listview.row;

import java.security.InvalidParameterException;

import org.liquidByte.trafficMustang.R;
import org.liquidByte.trafficMustang.androidApp.Bindings;
import org.liquidByte.trafficMustang.androidApp.listview.adapter.RowAdapter;
import org.liquidByte.trafficMustang.androidApp.services.dto.TrechoDTO;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

public class AceiteRow extends ModelListViewRow<TrechoDTO> implements OnClickListener {

	private TextView lblPassageiro;
	private TextView txvReputacao;
	private TextView lblSituacao;
	private ImageButton btnConfirmar;
	private ImageButton btnNegar;
	private RowActionListener<AceiteRow> confirmarActionListener;
	private RowActionListener<AceiteRow> negarActionListener;
	
	public AceiteRow(RowAdapter<TrechoDTO> parentAdadpter, View view) {
		super(parentAdadpter, view);
		if (view.getId() != R.layout.listview_row_aceite) {
			throw new InvalidParameterException("view is not a type of listview_row_aceite");
		}
	}
	
	public TextView getLblPassageiro() {
		if (this.lblPassageiro == null) {
			this.lblPassageiro = (TextView)this.getView().findViewById(R.id.row_aceite_lblPassageiro);
		}
		return lblPassageiro;
	}

	public TextView getTxvReputacao() {
		if (this.txvReputacao == null) {
			this.txvReputacao = (TextView)this.getView().findViewById(R.id.row_aceite_txvReputacao);
		}
		return txvReputacao;
	}

	public TextView getLblSituacao() {
		if (this.lblSituacao == null) {
			this.lblSituacao = (TextView)this.getView().findViewById(R.id.row_aceite_lblSituacao);
		}
		return lblSituacao;
	}

	public ImageButton getBtnConfirmar() {
		if (this.btnConfirmar == null) {
			this.btnConfirmar = (ImageButton)this.getView().findViewById(R.id.row_aceite_confirmar);
		}
		return btnConfirmar;
	}

	public ImageButton getBtnNegar() {
		if (this.btnNegar == null) {
			this.btnNegar = (ImageButton)this.getView().findViewById(R.id.row_aceite_negar);
		}
		return btnNegar;
	}

	public void setConfirmarActionListener(RowActionListener<AceiteRow> confirmarActionListener) {
		this.confirmarActionListener = confirmarActionListener;
	}

	public void setNegarActionListener(RowActionListener<AceiteRow> negarActionListener) {
		this.negarActionListener = negarActionListener;
	}

	@Override
	public void bindToView() {
		TrechoDTO trecho = (TrechoDTO)this.getModel();
		if (trecho == null) {
			this.getLblPassageiro().setText(null);
			this.getLblSituacao().setText(null);
			this.getTxvReputacao().setText(null);
		} else {
			if (trecho.getPassageiro() == null) {
				this.getLblPassageiro().setText(null);
				this.getTxvReputacao().setText(null);
			} else {
				this.getLblPassageiro().setText(trecho.getPassageiro().getNome());
				Bindings.percentSet(this.getTxvReputacao(), trecho.getPassageiro().getReputacao());
			}			
			this.getLblSituacao().setText(trecho.getSituacao());
		}
	}

	@Override
	public void init() {
		this.getBtnConfirmar().setOnClickListener(this);
		this.getBtnNegar().setOnClickListener(this);
	}

	@Override
	public void onClick(View view) {
		if (this.getBtnConfirmar() == view && this.confirmarActionListener != null) {
			this.confirmarActionListener.actionPerformed(this);
		} else if (this.getBtnNegar() == view && this.negarActionListener != null) {
			this.negarActionListener.actionPerformed(this);
		}
	}
}
